<template>
  <div class="app-container">
    <div class="filter-container">


    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="" label="ID" width="180">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="套餐信息" >
        <template slot-scope="{row}">
          <span>{{ row.package.title }}</span>&nbsp;

        </template>
      </el-table-column>
      <el-table-column align="center" label="价格" width="180">
        <template slot-scope="{row}">
          <span>￥{{ row.package.price }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="已买数量" width="180">
        <template slot-scope="{row}">
          <span>{{ row.sum_number }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="过期时间" width="220">
        <template slot-scope="{row}">
          <el-tag type="success" v-if="getTimeDifference(row.sum_valid_time) > 15">{{row.sum_valid_time}}</el-tag>
          <div style="color: red" v-if="getTimeDifference(row.sum_valid_time) <= 15 && getTimeDifference(row.sum_valid_time) > 0">
            {{row.sum_valid_time}}【即将过期，请及时续费，还有{{getTimeDifference(row.sum_valid_time)}}天过期】
          </div>
          <div style="color: red" v-if="getTimeDifference(row.sum_valid_time) <= 0">
            {{row.sum_valid_time}}【已经过期，请及时续费】
          </div>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="160">
        <template slot-scope="{row}">
          <router-link :to="'/order/order-package-edit/'+row.id">
            <el-button size="small" icon="el-icon-document">
              详情
            </el-button>
          </router-link>
          <router-link :to="'/order/create?package_id='+row.package_id">
            <el-button size="small" icon="el-icon-coin">
              续费
            </el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.per_page" @pagination="getList" />
  </div>
</template>

<script>
import { getOrderPackageList } from '@/api/order'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'OrderPackageList',
  components: { Pagination },
  filters: {},
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        per_page: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getOrderPackageList(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDataTypeFilter(val) {
      this.listQuery.data_type = val
      this.handleFilter()
    },
    getTimeDifference(sumValidTime) {
      var date1 = new Date(sumValidTime)
      var date2 = new Date() // 当前日期
      if (date1 < date2) {
        return 0
      }

      // 计算两个日期之间的毫秒数差值
      var timeDifference = Math.abs(date1 - date2)
      var daysDiff = Math.floor(timeDifference / (24 * 60 * 60 * 1000))
      console.log('两个日期相隔' + daysDiff + '天')

      return daysDiff
    }
  }
}
</script>

<style scoped>
  .edit-input {
    padding-right: 100px;
  }
  .cancel-btn {
    position: absolute;
    right: 15px;
    top: 10px;
  }
</style>
